import 'package:get/get.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/activity_stars_vo.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';

class XingxuanSpecialPageController extends GetxController {
  
  var tabIndex = 0.obs;
  var typeArr = <PriceArea>[].obs;
  var dataArr = <ActivityStarsVo>[].obs;

  /// 星选专区数据
  // var activityStarsVo = ActivityStarsVo(
  //   data: [],
  //   meta: ActivityStarsMeta(
  //     priceArea: [],
  //     pagination: CommonPagination(
  //       total: 0,
  //       currentPage: 1
  //     )
  //   )
  // ).obs;

  /// 选中的价格
  var price = ''.obs;
  
  @override
  void onReady() {
    getActivityStars();
    super.onReady();
  }

  /// 获取星选专区
  getActivityStars() async {
    ActivityStarsVo tempVo = await HttpUtilService.getActivityStars(page: 1, price: '');
    typeArr.value = tempVo.meta!.priceArea!;
    typeArr.forEach((item) {
      dataArr.add(ActivityStarsVo(
        data: [],
        meta: ActivityStarsMeta(
          priceArea: [],
          price: item.value,
          pagination: CommonPagination(
            total: 0,
            currentPage: 1
          )
        )
      ));
    });
    if (dataArr.length > 0) {
      dataArr[0] = tempVo;
    }
    update();

    // for (int i = 1; i < dataArr.length; i++) {
    //   ActivityStarsVo dataArrTemp = await HttpUtilService.getActivityStars(page: 1, price: typeArr[i].value!, loading: false);
    //   dataArrTemp.meta!.price = typeArr[i].value!;
    //   dataArr[i] = dataArrTemp;
    // }
    // update();
  }

  /// 重新获取某项数据
  getActivityStarsItem({ bool showLoading = false}) async {
    dataArr[tabIndex.value].meta!.pagination!.currentPage = 1;
    ActivityStarsVo dataArrTemp = await HttpUtilService.getActivityStars(page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, price: typeArr[tabIndex.value].value!, loading: showLoading);
    dataArrTemp.meta!.price = typeArr[tabIndex.value].value;
    dataArr[tabIndex.value] = dataArrTemp;
    update();
  }

  /// 加载更多星选专区
  onLoadActivityStars() async {
  //   if (activityStarsVo.value.meta!.pagination!.total! <= activityStarsVo.value.data!.length) {
  //     return;
  //   }
  //   activityStarsVo.value.meta!.pagination!.currentPage = activityStarsVo.value.meta!.pagination!.currentPage! + 1;
  //   ActivityStarsVo tempVo = await HttpUtilService.getActivityStars(page: activityStarsVo.value.meta!.pagination!.currentPage!, price: price.value);
  //   activityStarsVo.value.data!.addAll(tempVo.data!);
  //   update();
    if (dataArr[tabIndex.value].meta!.pagination!.total! <= dataArr[tabIndex.value].data!.length) {
      return;
    }
    dataArr[tabIndex.value].meta!.pagination!.currentPage = dataArr[tabIndex.value].meta!.pagination!.currentPage! + 1;
    ActivityStarsVo tempVo = await HttpUtilService.getActivityStars(page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, price: dataArr[tabIndex.value].meta!.price!);
    dataArr[tabIndex.value].data!.addAll(tempVo.data!);
    update();
  }
}